package server

import (
	"gitee.com/enjoy_5/coupons-rpc/internal/models"
	coupon "gitee.com/enjoy_5/coupons-rpc/proto/coupon"
)

func couponTeam(list []models.Coupon) (teamList []*coupon.CouponTeam) {
	for _, v := range list {
		teamList = append(teamList, &coupon.CouponTeam{
			Id:               int64(v.ID),
			UserID:           int64(v.UserID),
			CouponTitle:      v.CouponTitle,
			CouponIntroduce:  v.CouponIntroduce,
			CouponExpiration: v.CouponExpiration,
			CouponStatus:     int64(v.CouponStatus),
			DiscountValue:    float32(v.DiscountValue),
		})
	}
	return teamList
}

func couponModelInfo(info *models.Coupon) (teamList *coupon.CouponTeam) {
	return &coupon.CouponTeam{
		Id:               int64(info.ID),
		UserID:           int64(info.UserID),
		CouponTitle:      info.CouponTitle,
		CouponIntroduce:  info.CouponIntroduce,
		CouponExpiration: info.CouponExpiration,
		CouponStatus:     int64(info.CouponStatus),
		DiscountValue:    float32(info.DiscountValue),
	}
}

func TakeCouponInfoBYCouponID(couponID int64) *coupon.CouponTeam {
	m := new(models.Coupon)
	infoByCouponID, err := m.CouponInfoByCouponID(couponID)
	if err != nil {
		return nil
	}

	return couponModelInfo(infoByCouponID)
}

func TakeCouponAdd(userId int, pointsId int64) *coupon.CouponTeam {
	m := new(models.Coupon)
	couponAdd, err := m.CouponAdd(int64(userId), pointsId)
	if err != nil {
		return nil
	}
	return couponModelInfo(couponAdd)
}

func TakeCouponCreate(id int64, ruleInfo []models.Rule) []*coupon.CouponTeam {
	m := new(models.Coupon)
	CouponCreate, err := m.CouponCreate(id, ruleInfo)
	if err != nil {
		return nil
	}
	return couponTeam(CouponCreate)
}

func TakeCouponUser(userId int, couponStatus int) []*coupon.CouponTeam {
	m := new(models.Coupon)
	CouponUserList, err := m.CouponListUser(userId, couponStatus)
	if err != nil {
		return nil
	}
	return couponTeam(CouponUserList)
}

func TakeCouponShop(userId int, judge bool) []*coupon.CouponTeam {
	m := new(models.Coupon)
	CouponShopList, err := m.CouponListShop(userId, judge)
	if err != nil {
		return nil
	}
	return couponTeam(CouponShopList)
}
